<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="主题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="topic">
              <a-input v-model="model.topic" placeholder="请输入主题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="opration == 'send'">
            <a-form-model-item label="数据" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sendData">
              <a-input v-model="model.sendData" placeholder="请输入发送数据"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'YksTopicOprationForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      opration:{
        type:String,
        default:"send",
        required:true,
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           topic: [
              { required: true, message: '请输入主题!'},
           ],
           sendData: [
              { required: true, message: '请输入订阅数据!'},
           ],
        },
        url: {
          addTopic: "/mqtt/add_topic",
          deleteTopic: "/mqtt/delete_topic",
          sendTopicData: "/mqtt/send_data",   
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = 'get';
            let param = {}
            console.log(this.opration)
            if(this.opration == "add"){
              httpurl = this.url.addTopic;
              param = {topic: this.model.topic}
            }else if (this.opration == "delete"){
              httpurl = this.url.deleteTopic;
              param = {topic: this.model.topic}
            } else {
              httpurl = this.url.sendTopicData + "/" + this.model.topic;
              param = {'sendData': this.model.sendData}
            }
            getAction(httpurl,param).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>